package com.alex.dml;

import java.util.Arrays;
import java.util.Collection;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import org.apache.kafka.clients.admin.AdminClientConfig;
import org.apache.kafka.clients.admin.CreateTopicsResult;
import org.apache.kafka.clients.admin.DescribeTopicsResult;
import org.apache.kafka.clients.admin.KafkaAdminClient;
import org.apache.kafka.clients.admin.ListTopicsOptions;
import org.apache.kafka.clients.admin.ListTopicsResult;
import org.apache.kafka.clients.admin.NewTopic;
import org.apache.kafka.clients.admin.TopicDescription;
import org.apache.kafka.common.KafkaFuture;

/**
 * @ClassName KafkaTopicDMl
 * @Description TODO
 * @Author zjg
 * @Date 2021/12/613:45
 * @Version 2.0
 */
//必须将
public class KafkaTopicDMl {

  public static void main(String[] args) throws ExecutionException, InterruptedException {
    //1、创建kafkaAdminClient
    Properties props =new Properties();

    props.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG,"49.233.172.9:9092");

    //查看topic列表
    KafkaAdminClient adminClient = (KafkaAdminClient) KafkaAdminClient.create(props);
    //创建topic
    //    CreateTopicsResult createTopicsResult = adminClient.createTopics(Arrays.asList(new NewTopic("topic03",3,(short) 1)));
//    createTopicsResult.all().get();//同步创建的方式

    ListTopicsResult listTopicsOptions =adminClient.listTopics();
      Set<String> names = listTopicsOptions.names().get();
      for (String name:names){
        System.out.println(name);
      }
    //删除topic
//    adminClient.deleteTopics(Arrays.asList("topic01"));
      // 查看你topic 详细信息
    //查看topic信息
    DescribeTopicsResult topic01 = adminClient.describeTopics(Arrays.asList("topic01","topic02"));
    Map<String, TopicDescription> stringTopicDescriptionMap = topic01.all().get();
    Set<Entry<String, TopicDescription>> entries = stringTopicDescriptionMap.entrySet();
    for(Entry<String,TopicDescription> entry:entries){
      System.out.println(entry.getKey()+"\t"+entry.getValue());
    }
    //关闭AdminClient
    adminClient.close();

  }
}
